// server.js
const express = require('express');
const mysql = require('mysql2/promise');
const app = express();
const cors = require('cors');
app.use(cors());

// 数据库连接池
const pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '081114',
    database: 'agnsp'
});

// API端点
app.get('/api/gd', async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 1;
        const limit = parseInt(req.query.limit) || 15;
        const offset = (page - 1) * limit;
        
        console.log(`执行查询: LIMIT ${limit} OFFSET ${offset}`); // 调试日志
        
        const [rows] = await pool.query(
            'SELECT * FROM gd LIMIT ? OFFSET ?', 
            [limit, offset]
        );
        
        console.log('查询结果:', rows.length, '条记录'); // 调试日志
        
        const [countRows] = await pool.query('SELECT COUNT(*) as total FROM gd');
        const total = countRows[0].total;
        
        res.json({
            data: rows,
            total: total,
            page: page,
            totalPages: Math.ceil(total / limit),
            sampleData: rows.length > 0 ? rows[0] : null // 返回第一条样本数据供调试
        });
    } catch (error) {
        console.error('数据库错误:', error);
        res.status(500).json({ 
            error: 'Database error',
            details: error.message 
        });
    }
});
app.listen(1111, () => console.log('Server running on port 1111'));